//
//  LoginViewController.m
//  Guji
//
//  Created by 华谕 on 2017/9/4.
//  Copyright © 2017年 LXF. All rights reserved.
//

#import "LoginViewController.h"
#import "RetrievePasswordViewController.h"
#import "RegisteredViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>
{
    UITextField *loginPhoneField;
    UITextField *loginpasswField;
}
@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    
}

- (void)dismissVc
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)creatUI
{
    self.navigationItem.title = @"登录";
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:Icon(@"backimg") style:UIBarButtonItemStylePlain target:self action:@selector(dismissVc)];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
  
    NSArray *titleAry = @[@"账号",@"密码"];
//    NSArray *imgAry = @[@"logphone",@"logPassw"];
    NSArray *pladerAry = @[@"请输入手机号",@"请输入6-16位密码"];
    for (int i = 0; i < titleAry.count; i ++)
    {
//        UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(35*displaywidth,65*displaywidth+95*displaywidth*i,20*displaywidth,25*displaywidth) ImageName:imgAry[i]];
//        imgView.contentMode = UIViewContentModeScaleAspectFit;
//        [self.view addSubview:imgView];
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(60*displaywidth, 105*displaywidth+95*displaywidth*i, 70*displaywidth, 25*displaywidth) Font:15.0 Text:titleAry[i] Color:BlaTiColor];
        [self.view addSubview:titLab];
        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(60*displaywidth, 135*displaywidth+95*displaywidth*i, SCREEN_WIDTH-120*displaywidth, 40*displaywidth) placeholder:pladerAry[i] passWord:NO leftImageView:nil rightImageView:nil Font:15.0];
        [textfield setValue:RGB(204, 204, 204) forKeyPath:@"_placeholderLabel.textColor"];
        textfield.textColor = BlaTiColor;
        textfield.delegate = self;
        [self.view addSubview:textfield];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(60*displaywidth, 180*displaywidth+95*displaywidth*i, SCREEN_WIDTH-120*displaywidth, 1*displaywidth)];
        line.backgroundColor = LinesColor;
        [self.view addSubview:line];
        if (i == 0)
        {
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            loginPhoneField = textfield;
        }
        else
        {
            textfield.secureTextEntry = YES;
            loginpasswField = textfield;
        }
       
        
    }
    UIButton *forgotBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-160*displaywidth, 285*displaywidth, 100*displaywidth, 25*displaywidth) ImageName:nil Target:self Action:@selector(FindPassword) Title:@"忘记密码" Font:13.0];
    forgotBtn.contentHorizontalAlignment = 2;
    forgotBtn.contentVerticalAlignment = 0;
    [forgotBtn setTitleColor:RGB(153, 153, 153) forState:UIControlStateNormal];
    [self.view addSubview:forgotBtn];
    
    
    UIButton *loginBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth , 325*displaywidth, SCREEN_WIDTH-120*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(loginBtnBtnAction) Title:@"登录" Font:16.0];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.backgroundColor = ThemeColor;
    [loginBtn.layer setMasksToBounds:YES];
    [loginBtn.layer setCornerRadius:5*displaywidth];
    [self.view addSubview:loginBtn];
    
    UIButton *registeredBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth, 380*displaywidth, SCREEN_WIDTH-120*displaywidth, 40*displaywidth) ImageName:nil Target:self Action:@selector(registeredBtnAction) Title:@"还没有账号？去注册" Font:15.0];
    [registeredBtn setTitleColor:RGB(102, 102, 102) forState:UIControlStateNormal];
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"还没有账号？去注册"];
    NSRange titleRange = [[title string]rangeOfString:@"注册"];
    [title addAttribute:NSForegroundColorAttributeName value:ThemeColor range:titleRange];
    [title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
    registeredBtn.titleLabel.attributedText = title;
    [self.view addSubview:registeredBtn];

}









//找回密码
- (void)FindPassword
{
    RetrievePasswordViewController *Vc = [[RetrievePasswordViewController alloc] init];
    Vc.titleString = @"忘记密码";
    [self.navigationController pushViewController:Vc animated:YES];
}


//注册
- (void)registeredBtnAction
{
    RegisteredViewController *vc = [[RegisteredViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

//登录
- (void)loginBtnBtnAction
{
    [self.view endEditing:YES];
    if (loginPhoneField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入手机号" addToView:self.view];
        return;
    }
    if (loginpasswField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入密码" addToView:self.view];
        return;
    }
    
    [self requestPublicKey:loginpasswField.text];
}

- (void)requestPublicKey:(NSString *)passWorld
{
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:sharedDelegate.window animated:YES];
//    [HYBaseHandler requestPublicKeyParmers:nil Success:^(id obj)
//     {
//         if ([obj server_Getcodecheck])
//         {
//             NSString *publicKey = obj[@"data"];
//             //测试要加密的数据
//             NSString *sourceStr = passWorld;
//             //公钥加密
//             NSString *encryptStr = [RSA encryptString:sourceStr publicKey:publicKey];
//             [self logintoGuji:encryptStr];
//         }
//         else
//         {
//             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
//             [MBProgressHUD hideAllHUDsForView:sharedDelegate.window animated:YES];
//         }
//     } failed:^(NSError *error) {
//         [MBProgressHUD hideAllHUDsForView:sharedDelegate.window animated:YES];
//     }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
//登录
- (void)logintoGuji:(NSString *)passWord
{
    WEAKSELF;
    NSDictionary *dic = @{@"phone":loginPhoneField.text,
                          @"password":passWord};
    [LTools cache:@"" ForKey:SERVER_LoginToken];
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [HYBaseHandler requestUserLoginParmers:dic Success:^(id obj) {
//        [MBProgressHUD hideAllHUDsForView:sharedDelegate.window animated:YES];
//        if ([obj server_Getcodecheck])
//        {
//            [LTools storage:YES ForKey:SERVER_IsLogin];
//            [LTools cache:[NSString stringWithFormat:@"%@",obj[@"data"][@"provinceId"]] ForKey:SERVER_sheng];
//            [LTools cache:[NSString stringWithFormat:@"%@",obj[@"data"][@"cityId"]] ForKey:SERVER_shi];
//            [LTools cache:[NSString stringWithFormat:@"%@",obj[@"data"][@"districtId"]] ForKey:SERVER_qu];
//            [LTools cache:[NSString stringWithFormat:@"%@",obj[@"data"][@"provinceText"]] ForKey:SERVER_shengName];
//            [LTools cache:[NSString stringWithFormat:@"%@",obj[@"data"][@"cityText"]] ForKey:SERVER_shiName];
//            [LTools cache:[NSString stringWithFormat:@"%@",obj[@"data"][@"districtText"]] ForKey:SERVER_quName];
//            [LTools cache:[NSString stringWithFormat:@"%@",obj[@"data"][@"id"]] ForKey:SERVER_UserID];
//            [LTools cache:obj[@"data"][@"phone"] ForKey:SERVER_Phone];
//            [LTools cache:obj[@"data"][@"token"] ForKey:SERVER_LoginToken];
//            [LTools showMBProgressWithText:@"登录成功" addToView:sharedDelegate.window];
//            [weakSelf.navigationController popViewControllerAnimated:YES];
//        }
//        else
//        {
//            if ([obj[@"code"] integerValue] == 111)
//            {
//                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的账号已被冻结，请联系客服" preferredStyle:UIAlertControllerStyleAlert];
//                [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//                    
//                }]];
//                [alertVc addAction:[UIAlertAction actionWithTitle:@"联系客服" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",obj[@"msg"]]]];
//                }]];
//                [self presentViewController:alertVc animated:YES completion:nil];
//                return;
//            }
//            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
//        }
//    } failed:^(NSError *error) {
//        [MBProgressHUD hideAllHUDsForView:sharedDelegate.window animated:YES];
//
//    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
